//--------------------------------------------------- // Purpose: Program to calculate magic numbers // Author: John Gauch //--------------------------------------------------- #include using namespace std; // Function to calculate magic number int Magic(int Number) { // Get digits and calculate sum int Sum = 0; while (Number > 0) { int Digit = Number % 10; Sum = Sum + Digit; Number = Number / 10; } return Sum; } // Main body of program int main() { // Get user input int Input, Output; cout << "Enter a positive integer: "; cin >> Input; // Do error checking if (Input < 0) cout << "Error: Number must be positive" << endl; // Calculate magic number and print results else { cout << "Input = " << Input << endl; Output = Magic(Input); cout << "Output = " << Output << endl; while (Output > 9) { Output = Magic(Output); cout << "Output = " << Output << endl; } } return 0; }